package com.kk.permission.utils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.util.Log;

import com.kk.permission.adapter.PermissionAdapter;
import com.kk.permission.huawei.HuaweiPermissionGuideStrategy;

import java.util.HashSet;
import java.util.Locale;

public class PackageUtil {
    public static final String HUAWEI_PHONE_MANAGER = "com.huawei.systemmanager";
    public static final String MEIZU_PHONE_MANAGER = "com.meizu.safe";
    public static final String[] OPPO_2_0_PERMISSION_SETTING_PACKAGE_NAMES = new String[]{"com.oppo.safe"};
    public static final String[] OPPO_COLOROS_PERMISSION_SETTING_PACKAGE_NAMES = new String[]{"com.coloros.safecenter"};
    public static final String[] OPPO_PERMISSION_SETTING_PACKAGE_NAMES = new String[]{"com.color.safecenter"};
    public static String PACKAGE = "package";
    public static String SAMSING_Settings = "com.android.settings";
    public static String SAMSUNG_APPLICATION_DETAILS_SETTINGS = "android.settings.APPLICATION_DETAILS_SETTINGS";
    public static final String[] SAMSUNG_PERMISSION_SETTINGS_PACKAGE_NAMES = new String[]{"com.samsung.memorymanager", "com.samsung.android.sm", "com.samsung.android.svoice", "com.samsung.networkui", "com.samsung.android.contacts", "com.samsung.android.SettingsReceiver", "com.samsung.android.sm_cn"};
    public static String SAMSUNG_S6_AppDrawOverActivity = "com.android.settings.Settings$AppDrawOverlaySettingsActivity";
    public static String SAMSUNG_S6_HighPowerApplicationsActivity = "com.android.settings.Settings$HighPowerApplicationsActivity";
    public static String SAMSUNG_S6_InstalledAppDetailsTop = "com.android.settings.applications.InstalledAppDetailsTop";
    public static String SAMSUNG_S6_MANAGER = "com.samsung.android.sm.ui.dashboard.cstyle.SmartManagerDashBoardActivity";
    public static String SAMSUNG_S7_AppDrawOverActivity = SAMSUNG_S6_AppDrawOverActivity;
    public static String SAMSUNG_S7_MANAGER = "com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity";
    public static String SAMSUNG_S7_Notification = "com.android.settings.Settings$BlockNotificationListActivity";
    public static String SAMSUNG_SM = "com.samsung.android.sm";
    public static String SAMSUNG_SM_CN = "com.samsung.android.sm_cn";
    public static String SAMSUNG_Settings$SecuritySettingsActivity = "com.android.settings.Settings$SecuritySettingsActivity";
    public static String SAMSUNG_SmartManagerDashBoardActivity = "com.samsung.android.sm.app.dashboard.SmartManagerDashBoardActivity";
    private static final String TAG = "PackageUtil";
    public static final String VIVO_PHONE_MANAGER = "com.iqoo.secure";
    public static final String[] ZTE_SETTINGS_PACKAGE_NAMES = new String[]{"com.zte.heartyservice", "com.zte.smartpower"};
    private static HashSet<String> sInstalledPackageSet = new HashSet();
    private static Boolean sIsAutoPermission = null;
    private static boolean sIsQueryingInstalledPackage = false;

    @SuppressLint("WrongConstant")
    public static boolean isIntentAvailable(Intent intent) {
        try {
            if (PermissionAdapter.getAdapter().getAppContext().getPackageManager().queryIntentActivities(intent, 65536).size() > 0) {
                return true;
            }
        } catch (Exception unused) {

        }
        return false;
    }

    public static boolean isPackageInstalled(String str) {
        if (sIsQueryingInstalledPackage || sInstalledPackageSet.size() <= 0) {
            return doIsPackageInstalled(str);
        }
        return sInstalledPackageSet.contains(str);
    }

    @SuppressLint("WrongConstant")
    public static boolean doIsPackageInstalled(String paramString)
    {
        try
        {
            PermissionAdapter.getAdapter().getAppContext().getPackageManager().getPackageInfo(paramString, 1);
            return true;
        }
        catch (Exception e)
        {
        }
        return false;
    }

    public static boolean isPermissionGuideOverview()
    {
        if ((!OSUtil.isMiuiV6()) && (!OSUtil.isMiuiV7()) && (!OSUtil.isMiuiV10()) && (!OSUtil.isXiaomiMi4cAndroid7()) && (!OSUtil.isRedMiNote4XAndroid7()) && (!OSUtil.isRedMiNote5Android8()))
        {
            if (OSUtil.isRedMi5PlusAndroid7()) {
                return false;
            }
//            if (OSUtil.isSamsung5()) {
//                return false;
//            }
//            if (OSUtil.isOppoR11stAndroid7()) {
//                return false;
//            }
            return !OSUtil.isHuaweiALP_AL00();
        }
        return false;
    }

    public static boolean isAutoPermission() {
        if (sIsAutoPermission == null) {
            sIsAutoPermission = Boolean.valueOf(queryAutoPermission());
        }
        return sIsAutoPermission.booleanValue();
    }

    public static boolean OppoColorOSPermissionGuide_isAutoPermission() {
        String str = Build.DISPLAY;
        String str2 = TAG;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("isAutoPermission_display : ");
        stringBuilder.append(str);
        Log.i(str2, stringBuilder.toString());
        return true;
    }

    public static boolean queryAutoPermission() {
//        if ((OSUtil.isSamsung6() || OSUtil.isSamsung7()) && !"SM-N9500".equals(Build.MODEL)) {
//            return !"SM-C9000".equals(Build.MODEL) || Build.TIME < 1504257263000L;
//        } else {
            if (!isIntentAvailable(new Intent("android.settings.ACCESSIBILITY_SETTINGS"))) {
                return false;
            }
            /*if (isPackageInstalled(OPPO_COLOROS_PERMISSION_SETTING_PACKAGE_NAMES) || isPackageInstalled(OPPO_PERMISSION_SETTING_PACKAGE_NAMES) || isPackageInstalled(OPPO_2_0_PERMISSION_SETTING_PACKAGE_NAMES)) {
                if (OppoColorOSPermissionGuideStrategy.isAutoPermission()) {
                    return true;
                }
                if (OppoColorOSPermissionGuide_isAutoPermission()) {
                    return true;
                }
                String str;
                StringBuilder stringBuilder;
                try {
                    String str2 = Build.DISPLAY;
                    str = TAG;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("display=");
                    stringBuilder.append(str2);
                    TLog.i(str, stringBuilder.toString(), new Object[0]);
                    String[] split = str2.split("_");
                    str2 = split[split.length - 1];
                    return (Integer.parseInt(str2) < 170500 || Integer.parseInt(str2) >= 170603) && Integer.parseInt(str2) <= 170705;
                } catch (Exception e) {
                    str = TAG;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append(e);
                    stringBuilder.append("");
                    TLog.e(str, stringBuilder.toString(), new Object[0]);
                }
            } else */if (isPackageInstalled("com.huawei.systemmanager")) {
                if (Build.VERSION.SDK_INT >= 19 && new HuaweiPermissionGuideStrategy(PermissionAdapter.getAdapter().getAppContext()).supportAutoPermission()) {
                    return Locale.getDefault().getLanguage().equals("en") || (Locale.getDefault().getLanguage().equals("zh") && Locale.getDefault().getCountry().equals("CN"));
                } else {
                    return false;
                }
            } /*else if (OSUtil.isMiuiV6() || OSUtil.isMiuiV7()) {
                return false;
            } *//*else {
                if (OSUtil.isMiuiV8()) {
                    if (Build.TIME < 1503128052000L && !VERSION.INCREMENTAL.startsWith("V8.2")) {
                        return Locale.getDefault().getLanguage().equals("en") || (Locale.getDefault().getLanguage().equals("zh") && Locale.getDefault().getCountry().equals("CN"));
                    } else {
                        return false;
                    }
                } else if (OSUtil.isMiuiV9() || OSUtil.isMiuiV10()) {
                    return true;
                } else {
                    if (isPackageInstalled("com.meizu.safe")) {
                        return MeizuPermissionGuideStrategy.isAutoPermission();
                    }
                    return OnePlusStrategyGenerator.canThisPhoneUseAutoMode();
                }
            }*/
//        }
        return false;
    }

    public static boolean isPackageInstalled(String[] strArr) {
        boolean z = false;
        if (strArr == null || strArr.length == 0) {
            return false;
        }
        for (String isPackageInstalled : strArr) {
            if (isPackageInstalled(isPackageInstalled)) {
                z = true;
                break;
            }
        }
        return z;
    }

    public static String getVersionName(String str) {
        String str2 = "";
        try {
            return PermissionAdapter.getAdapter().getAppContext().getPackageManager().getPackageInfo(str, 0).versionName;
        } catch (Throwable e) {
            return str2;
        }
    }

    public static String getAppName(String str) {
        ApplicationInfo applicationInfo;
        PackageManager packageManager = PermissionAdapter.getAdapter().getAppContext().getPackageManager();
        try {
            applicationInfo = packageManager.getApplicationInfo(str, 0);
        } catch (NameNotFoundException unused) {
            applicationInfo = null;
        }
        return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "(unknown)");
    }
}
